/* 
 * File:   Loader.cpp
 * Author: guilhermemg
 * 
 * Created on 12 de Fevereiro de 2012, 14:54
 */

#include "Loader.h"

bool compareAsInts(string a, string b);

Loader::Loader(const char* p) {
    this->path = p;
}

Loader::Loader(const Loader& orig) {
}

Loader::~Loader() {
}

/**
 * Return an array of IplImage
 * with the images contained in
 * pathDefault
 * 
 * @param begin
 * @param end
 * @return arrayImages
 */
vector<string> Loader::load() {
    Directory d(this->getPath());
    vector<string> vecNames = d.readDirectory();
    vector<string> vectorImagesPaths;
    
    //cout << "Ordering paths of images" << endl;
    sort(vecNames.begin(), vecNames.end(), compareAsInts);
    
    for(unsigned int i = 0; i < vecNames.size(); i++) { /*i < 100*/
        string pathStr = this->getPath();
        string nameStr = string(vecNames.at(i));
        string completePath = pathStr.append(nameStr);
        
        // converting string to char* !!!!
        const char* completePathChar;
        completePathChar = completePath.c_str();
        //cout << "complete path: " << completePathChar << endl;
        
        //cout << "Load Image: " << completePathChar << endl;
        vectorImagesPaths.push_back(completePathChar);
    }
    
    return vectorImagesPaths;
}

bool compareAsInts(string a, string b) {
    string digitsA;
    string digitsB;
    
    int i = 5, j = 5;
    char d1 = a.c_str()[i],d2 = b.c_str()[j];
    
    while(d1 != '.' || d2 != '.') {
        if(d1 != '.') {
            digitsA += d1;
            i++;
            d1 = a.c_str()[i];
        }
        if(d2 != '.') {
            digitsB += d2;
            j++;
            d2 = b.c_str()[j];
        }
    }
    int num1 = atoi(digitsA.c_str());
    int num2 = atoi(digitsB.c_str());
    
    return num1 < num2;
}

const char* Loader::getPath() {
    return path;
}
